package ajax;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

@WebServlet("/ajaxjson1")
public class AjaxJson1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String user = req.getParameter("user");
        System.out.println("user = " + user);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(req.getInputStream(), "UTF-8")
        );
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
//       1.获取了前端传递的对象字符串
        String s = stringBuilder.toString();
//       2.字符串转成java中json对象
        JSONObject jsonObject = JSONObject.parseObject(s);
//       3.拆解各项数据
        String name1 = jsonObject.getString("name1");
        String pwd1 = jsonObject.getString("pwd1");
//       查数据库
        System.out.println("name1 = " + name1);
        System.out.println("pwd1 = " + pwd1);
        System.out.println("s = " + s);
        PrintWriter writer = resp.getWriter();
        writer.print(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
